/* Andreas Baum
 * IMClientApp.java
 * Handles the connection of an IM client to the IM server
 * CS 78 - Networks - Programming Assignment 1
 */


package jbpClientTester;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


import com.apple.dnssd.DNSSDException;





public class JBPClientTester {

	public static int SERVICE_PORT = 0;
	public static int SERVER_PORT = 4444;

	private static Socket extSocket;
	private static String host;

	public JBPClientTester() {

	}

	public static void main(String args[]) throws IOException {

		BufferedReader keyboardIn =
			new BufferedReader(
					new InputStreamReader( System.in ) );

		System.out.println("\t*** Welcome to the Bonjour Proxy Client! ***");

		System.out.print("Please enter the address of the host >> ");
		host = keyboardIn.readLine();


		System.out.println("Attempting to connect to " + host + "...");
		try {
			// send connection request:
			extSocket = new Socket(host, SERVER_PORT);
		} catch (UnknownHostException e) {
			// If host can't be found:
			System.err.println("ERROR: Don't know about host: " + host + ".");
			System.exit(1);
		} catch (IOException e) {
			// If host doesn't accept connections:
			System.err.println("ERROR: Couldn't get I/O for "
					+ "the connection to: " + host + ".");
			System.exit(1);
		}

		// connection is established, hand it over to a client thread.
		DataThread cExtThread = new DataThread(extSocket, "cExtThread");

		// Confirm connection:
		System.out.println("Connected to " + host 
				+ " on port " + SERVER_PORT + ".");


//		Register iTunes service listener, and establish connection to iTunes:
		System.out.println("Registering iTunes service.");
		String serviceName = "_daap._tcp";
		String otherName = "CLICK ME!! (JBP CLIENT)";

		// the port is allocated by the system:
		ServerSocket serviceSocket = new ServerSocket(SERVICE_PORT);


		try
		{
			// Register the daap service:
			ServiceRegistrar iTunesRegistrar = new ServiceRegistrar(otherName, 
					serviceName, serviceSocket.getLocalPort());
			while (!iTunesRegistrar.isRegistered());
			// start a new thread on the socket as soon as iTunes sends
			// something back:

			DataThread serviceThread = new DataThread(serviceSocket.accept(), "iTunesServiceThread");
			serviceThread.setOtherThread(cExtThread);
			cExtThread.setOtherThread(serviceThread);

		} catch (InterruptedException e) {
			System.err.println("InterruptedException: " + e);
		}
		catch (DNSSDException e){
			System.err.println("DNSSDException: " + e);
		}
		catch (IOException e) {
			System.out.println("Accept failed: " + serviceSocket.getLocalPort());
			System.exit(-1);
		}


/*		String answer = "no";

		while (!(answer.toLowerCase().equals("y") || answer.toLowerCase().equals("yes"))) { 
			System.out.print("Is library loaded? enter y for yes\n>>> ");
			answer = keyboardIn.readLine();
		}

		System.out.println("Ok.");
		
		DataThread streamingThread = new DataThread(serviceSocket.accept(), "StreamingThread");
		System.out.println("Accepting new request!!");
		streamingThread.setOtherThread(cExtThread);
		cExtThread.setOtherThread(streamingThread);*/

//		System.out.println("Client-side pipeline established.");

	}

}